/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package SecretSanta.service.impl;

import SecretSanta.core.HistoryMap;
import SecretSanta.core.Person;
import SecretSanta.service.SantaParser;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;

import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Element;
import org.xml.sax.SAXException;

/**
 *
 * @author Mallisa
 */
public class SantaParserDomImpl implements SantaParser {
    
    private String HISTORYPATH = "src/resources/HistoryData.xml";
    private String[] PERSONELEM = {"name", "email", "restriction"};
    private String[] MAPPINGELEM = {"santa","reciever"};
    
    private Document data;

    @Override
    public List<HistoryMap> getHistory(int start, int end) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public HistoryMap getLatestHistory() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public List<Person> getPeople(List<String> people) {
        if (data == null) {
            readHistory();
        }
        NodeList nList = data.getElementsByTagName("person");
        System.out.println("----------------");
            
        for ( int i = 0; i < nList.getLength(); i++ ) {
            Node n = nList.item(i);
            if (n.getNodeType() == Node.ELEMENT_NODE) {
                Element e = (Element) n;
                for (String a : PERSONELEM) {
                    List<String> values = getTagValue(a, e);
                    for (String v : values) {
                        System.out.println(a + ": " + v);
                    }
                }
            }
        }
        return null;
    }

    @Override
    public Person getPerson(String person) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public Boolean addHistoryMap(HistoryMap history) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public Boolean updatePeople(List<Person> people) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
    
     /**
     * @param args
     */
    public static void main(String[] args) {
	SantaParserDomImpl parser = new SantaParserDomImpl();
	//parser.readHistory();
        parser.getPeople(null);
    }
    
    public void readHistory() {
        try {
            File history = new File(HISTORYPATH);
            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
            data = dBuilder.parse(history);
            data.getDocumentElement().normalize();
            
            System.out.println("Root element: " + data.getDocumentElement().getNodeName());
           
        } catch (SAXException ex) {
            System.out.println("SAX Error: " + ex.getMessage());
            //Logger.getLogger(SantaParserDomImpl.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            System.out.println("IO Error: " + ex.getMessage());
            //Logger.getLogger(SantaParserDomImpl.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ParserConfigurationException ex) {
            System.out.println("Parse Error: " + ex.getMessage());
            //Logger.getLogger(SantaParserDomImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    private List<String> getTagValue( String tag, Element e) {
        List<String> results = new ArrayList<String>();
        NodeList eList = e.getElementsByTagName(tag);
        for (int i = 0; i < eList.getLength(); i++) {
            Node nValue = (Node) eList.item(i).getChildNodes().item(0);
            results.add(nValue.getNodeValue());
        }
        
        return results;
    }
    
}
